<template>
	<div>
		<el-select
		    style="width: 400px;"
		    @change="changeSelect"
		    size="mini"
		    v-model="goods_name"
			:disabled="is_disabld==1"
		    filterable
		    remote
		    reserve-keyword
		    placeholder="请输入商品名称"
		    :remote-method="remoteMethod"
		    :loading="loading">
		    <el-option
		      v-for="item in options"
		      :key="item.goods_id"
		      :label="item.goods_name"
		      :value="item.goods_name">
		    </el-option>
		  </el-select>
	</div>
</template>

<script>
	export default {
		data(){
			return {
				 loading: false,
				 options:[],
				 goods_name:"",
				 rowIndex:0,
				 brand:"",
				 goods_type:"",
				 is_disabld:0
			}
		},
		props:{
			scopeData:{
				type:Object,
				default:{}
			}
		},
		watch:{
			// goods_name(newVal){
			// 	let obj = {
			// 		rowIndex:this.rowIndex,
			// 		goods_name:this.goods_name,
			// 		brand:this.brand,
			// 		goods_type:this.goods_type
			// 	}
			// 	this.$emit('getGoodsName',obj)
			// }					  
		},
		mounted() {
			this.rowIndex = this.scopeData.$index
			this.is_disabld = this.scopeData.row.is_disabld
			setTimeout(()=>{
				this.goods_name = this.scopeData.row.goods_name
			},500)
		},
		methods:{
			changeSelect(val){
				for(let i in this.options){
					if(this.options[i].goods_name == val){
						this.goods_type = this.options[i].goods_type
						this.brand = this.options[i].brand
						this.goods_name = this.options[i].goods_name
					}
				}
				let obj = {
					rowIndex:this.rowIndex,
					goods_name:this.goods_name,
					brand:this.brand,
					goods_type:this.goods_type
				}
				this.$emit('getGoodsData',obj)
			},
			remoteMethod(keywords) {
				if (keywords !== '') {
					this.loading = true
				  this.$post('/goods/searchGoodsInventory',{keywords},res=>{
					  this.loading = false
					  let result = res.data.data
					  if(result.length==1){
						  this.goods_name = result[0].goods_name
						  this.brand = result[0].brand
						  this.goods_type = result[0].goods_type
						  let obj = {
						  	rowIndex:this.rowIndex,
						  	goods_name:this.goods_name,
						  	brand:this.brand,
						  	goods_type:this.goods_type
						  }
						  this.$emit('getGoodsData',obj)
					  }
					  if(result.length>1){
						  result = this.$utils.unique(result,'goods_name')
					  }
					  this.options = result
				  })
				} else {
				  this.options = [];
				}
		  }
		}
	}
</script>

